<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\Myusers;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Auth;
use Illuminate\Http\Request;
use App;

class LoginController extends Controller
{

    public function __construct()
    {
        $this->middleware('admin.auth')->except(['login', 'loginPost']);
    }

    /**
     * LoginController constructor.
     */

    public function index()
    {
        $path='./apkinfo.zip';
        dug(jieya($path));
        dug(readzip($path));
        return view('admin.login.index');
//        dd(Auth::guard('admin')->user());
    }

    /**登录页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function login()
    {
        return view('admin.login.login');
    }

    /**登录数据处理
     * @return string
     */
    public function loginPost(Request $request)
    {
        // 1、校验信息测试
        $messages = [
            'username.required' => '用户名必须填写',
            'password.required' => '密码必须填写',
        ];
        $validator = Validator::make($request->all(), [
            'username' => 'required',
            'password' => 'required',
        ], $messages);
        if ($validator->fails()) {
            return redirect('/admin/login')->with(['errors' => $validator->errors(), 'username' => $request->username]);
        }
        $status = auth::guard('admin')->attempt([
            'username' => $request->input('username'),
            'password' => $request->input('password'),
        ]);
        if ($status) {
            return redirect('/admin/index');
        } else {
            return redirect('/admin/login')->with([
                'error' => '用户名或密码错误',
                'username' => $request->username
            ]);
        }
    }

    /**
     *退出操作
     */
    public function logout()
    {
        Auth::guard('admin')->logout();
        return redirect('admin/login');
    }

    public function test()
    {
        sleep(8);
        echo 1;
        exit;
    }

    public function update($id)
    {
        echo $id;
        exit;
    }

    public function destroy($id)
    {
       return response()->json([
           'code'=>0,
           'msg'=>'success',
           'data'=>[
               'statue'=>1
           ]
       ]);
    }
}
